<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="推荐人姓名" prop="recommendNameFr" label-width="100px">
        <el-input
          v-model="queryParams.recommendNameFr"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="律师姓名" prop="recommendLawyerName">
        <el-input
          v-model="queryParams.recommendLawyerName"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status" label-width="100px">
        <el-select v-model="queryParams.status" placeholder="请选择">
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="已通过" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="填表人信息" width="200">
        <template slot-scope="scope">
          <div style="font-size: 12px; line-height: 1.5;">
          填表人姓名：{{ scope.row.recommendNameFr }} <br>
          所在事务所：{{ scope.row.recommendCompanyFr }} <br>
          职务：{{ scope.row.recommendJoblevelFr }} <br>
          电话：{{ scope.row.recommendPhoneFr }} <br>
          邮箱：{{ scope.row.recommendMail }} <br>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="推荐律师">
        <template slot-scope="scope">
          <div style="font-size: 12px; line-height: 1.5;">
          律师姓名：{{ scope.row.recommendLawyerName }} <br>
          所属律所：{{ scope.row.recommendLawyerCompany }} <br>
          职务：{{ scope.row.recommendLawyoerJoblevel }} <br>
          从业年限：{{ scope.row.recommendLawyoerPeriod }} <br>
          工作城市：{{ scope.row.recommendLawyoerCity }} <br>
          律师手机：{{ scope.row.recommendLawyerPhone }} <br>
          律师邮箱：{{ scope.row.recommendLawyerMail }} <br>
          执业领域：{{ scope.row.recommendLawyerField }} <br>
          申报奖项：{{ scope.row.recommendLawyerAward }} <br>
          榜单内设分组：{{ scope.row.recommendLawyerGroup }} <br>
          团队总人数: {{ scope.row.teamSize  }} <br>
          执牌律师人数: {{ scope.row.licenseLawyers  }} <br>
          合伙人人数: {{ scope.row.partnerNum  }} <br>
          是否为团队负责人: {{ scope.row.managerFlag ? '是' : '否'  }} <br>
          过去三年平均的团队总营收: {{ scope.row.companyRevenue  }} <br>
          主要合作客户：<br> <div v-for="(item, index) in JSON.parse(scope.row.keyClients||'[]')" :key="item.key">{{index+1}}、客户名称:{{ item.name }} 所属行业：{{ item.industry }}</div>
          项目/案例：
          <div v-for="(item, index) in JSON.parse(scope.row.typicalCase||'[]')">
            {{index+1}}、名称：{{ item.name }}；简介：{{ item.desc }}；其他参与者：{{ item.desc }}；联系方式：{{ item.email }}；
          </div>
          推荐理由：{{ scope.row.recommendCon }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createdAt" width="160">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="验证状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row, 'status')"></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            class="text-red"
            icon="el-icon-remove-outline"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="办公地址" prop="lawfirmAddress">
          <el-input v-model="form.lawfirmAddress" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="电话" prop="lawfirmContact">
          <el-input v-model="form.lawfirmContact" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="手机号（账号）" prop="lawfirmTelnum">
          <el-input v-model="form.lawfirmTelnum" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="邮箱" prop="lawfirmEmail">
          <el-input v-model="form.lawfirmEmail" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="官网网址" prop="lawfirmWebsite">
          <el-input v-model="form.lawfirmWebsite" placeholder="请输入" />
        </el-form-item>
        <!-- TODO 关键指标--测试 -->

        <el-form-item label="律所简介" prop="lawfirmSummary">
          <el-input v-model="form.lawfirmSummary" type="textarea" :rows="3" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1"></el-input-number>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList, getItem, delItem, addItem, updateItem } from "@/api/mobile/recommend/month"

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchtxt: null,
      },
      recommendTypeMap: {
        1: '客户推荐人',
        2: '同行推荐人'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imgAction: `${process.env.VUE_APP_BASE_API}/common/ftpftpCommonUpload`,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getLevelFormat(status) {
      const dart = this.levelList.find(item => item.id === status);
      if (dart) {
        return dart.level;
      } else {
        return '未知';
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        avatar: null,
        nickname: null,
        realname: null,
        userLevel: null,
        cheese: null,
        integral: null,
        phone: null,
        lawyerJoblevel: null,
        isAdmin: null,
        sort: null,
        status: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = `修改信息`
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【信息编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/amini/recommendMonthPar/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row, field='status') {
      let text = row[field] == 0 ? "不通过" : "通过"
      this.$modal.confirm(`确认要"${text}"验证吗？`).then(function() {
        return updateItem({id :row.id, [field]: row[field]})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row[field] = row[field] == "0" ? 1 : 0
      })
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadSuccess(res){
      this.form.lawfirmLogo = res.path;
    }
  }
}
</script>

<style scoped>
.more-radio-group .el-radio{
  margin-top: 10px;
}
.other-input {
  margin-top: 8px;
}
</style>
